package com.Jian.reggie.service.impl;

import com.Jian.reggie.dto.SetmealDto;
import com.Jian.reggie.impl.SetmealDish;
import com.Jian.reggie.service.SetmealDishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.Jian.reggie.impl.Setmeal;
import com.Jian.reggie.service.SetmealService;
import com.Jian.reggie.mapper.SetmealMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 12646
 * @description 针对表【setmeal(套餐)】的数据库操作Service实现
 * @createDate 2023-11-27 15:07:43
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService {
  @Autowired
  private SetmealDishService setmealDish;
  
  @Override
  public void saveWithSetMeal(SetmealDto setmealDto) {
    this.save(setmealDto);
    setmealDto.getSetmealDishes().forEach(setmealDish -> {
      setmealDish.setSetmealId(setmealDto.getId());
    });
    setmealDish.saveBatch(setmealDto.getSetmealDishes());
  }
  
  @Override
  public void updateWithSetMeal(SetmealDto setmealDto) {
    this.updateById(setmealDto);
    LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
    setmealDish.remove(queryWrapper);
    setmealDto.getSetmealDishes().forEach(setmealDish -> {
      setmealDish.setSetmealId(setmealDto.getId());
    });
    
    setmealDish.saveBatch(setmealDto.getSetmealDishes());
    
  }
}




